﻿using BuyWithoutLoseMoney;
using BuyWithoutLoseMoney.Core.Models;
using BuyWithoutLoseMoney.Core.Repositories;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading.Tasks;

namespace BuyWithoutLose.Areas.Admin.Controllers
{
    /// <summary>
    /// 推荐位置
    /// </summary>
    [Area("admin")]
    public class PositionController : BaseAdminController
    {
        private readonly IRepository<Position, int> _positionRepository;
        private readonly IRepository<ResPosition, int> _resPositionRepository;
        private readonly IRepository<Product, int> _productRepository;
        public PositionController(IRepository<Position, int> positionRepository, 
            IRepository<ResPosition, int> resPositionRepository,
            IRepository<Product, int> productRepository, BuyWithoutLoseMoneyDBContext db) : base(db)
        {
            _positionRepository= positionRepository;
            _resPositionRepository= resPositionRepository;
            _productRepository= productRepository;
        }
        #region 位置管理
        /// <summary>
        /// 位置列表展示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Index()
        {
            var positions = _positionRepository.GetAllList();
            ViewBag.Position = positions;
            return View();
        }
        /// <summary>
        /// 新闻列表的查询
        /// </summary>标题和作者，分类
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Index(int page, int limit,int posType=0)
        {

            var resPositions = _resPositionRepository.GetAll();
            var query = resPositions.Include("Position").Where(x => true);
            //分类Id
            if (posType != 0)
            {
                query = query.Where(x => x.PositionId == posType);
            }
            var count = query.Count();
            var list = query.OrderBy(x => x.Id).Skip((page - 1) * limit).Take(limit).ToList()
               .Select(x => new
               {
                    id=x.Id,
                    position=x.Position.Name,
                    resId=x.ResId
               });
            return Json(new { Code = 0, Msg = "", Data = list, Count = count });
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Create(int id)
        {
            var product = _productRepository.GetAllList().FirstOrDefault(x=>x.Id==id);
            ViewBag.Product = product;
            var resposition = _resPositionRepository.GetAllList();
            ViewBag.ResPosition = resposition;
            var position = _positionRepository.GetAllList();
            ViewBag.Position = position;
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> Create(ResPosition resPosition)
        {
            //news.CreateTime = DateTime.Now;
            if (ModelState.IsValid)
            {
                await _resPositionRepository.InsertAsync(resPosition);
                return Json(new { isSuccess = true, msg = "添加成功，页面即将刷新！" });
            }
            return Json(new { isSuccess = false, msg = "添加失败，页面即将返回！" });
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Edit(int id)
        {
            var resPositions = _resPositionRepository.GetAllList().FirstOrDefault(x => x.Id == id);
            //var s = resPositions;
            var product = _productRepository.GetAllList();
            ViewBag.Product = product;
            var position = _positionRepository.GetAllList();
            ViewBag.Position = position;
            return View(resPositions);
        }
        [HttpPost]
        public async Task<IActionResult> Edit(ResPosition resPosition)
        {
            if (ModelState.IsValid)
            {
                await _resPositionRepository.UpdateAsync(resPosition);
                return Json(new { isSuccess = true, msg = "修改成功，页面即将刷新！" });
            }
            return Json(new { isSuccess = false, msg = "修改失败，请重试！" });
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> Del(int id)
        {
            ResPosition p = new ResPosition() { Id = id };
            if (ModelState.IsValid)
            {
                await _resPositionRepository.DeleteAsync(p);
                return Json(new { isSuccess = true, msg = "修改成功，页面即将刷新！" });
            }
            return Json(new { isSuccess = false, msg = "删除失败，请重试！" });
        }
        #endregion
    }
}
